/******************************************************************************
 * 
 * Announce: CSharpKit, Basic algorithms, components and definitions.
 *           Copyright (C) ShenYongchen.
 *           All rights reserved.
 *   Author: 申永辰.郑州 (shenyczz@163.com)
 *  WebSite: http://gitee.com/shenyczz/csharp_csk
 *
 * THIS CODE IS LICENSED UNDER THE MIT LICENSE (MIT).
 * THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF 
 * ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
 * IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
 * PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
 * 
******************************************************************************/

using System;
using System.Runtime.InteropServices;

namespace CSharpKit
{
    [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Auto, Pack = 1, Size = 2)]
    public struct UnionBytesUInt16 : IUnion
    {
        [FieldOffset(0)]
        byte b0;
        [FieldOffset(1)]
        byte b1;

        [FieldOffset(0)]
        UInt16 Value;


        public byte[] GetBytes()
        {
            return new byte[] { b0, b1 };
        }
        public void SetBytes(byte[] bs, int offset)
        {
            try
            {
                b0 = bs[offset + 0];
                b1 = bs[offset + 1];
            }
            catch (System.Exception)
            {
                // throw;
            }
        }

        public T GetValue<T>()
        {
            return (T)(dynamic)Value;
        }


        public void SetValue<T>(T value)
        {
            Value = (dynamic)value;
        }

        public static explicit operator UInt16(UnionBytesUInt16 union) => union.Value;
        public static explicit operator UnionBytesUInt16(UInt16 value) => new UnionBytesUInt16() { Value = value };

        // {{END}}
    }


}
